<template>
	<view class="content">
		<view class="header">
			<view class="header-item">抬头类型</view>
			<view class="header-item">
				<u-radio-group v-model="type">
					<u-radio active-color="#14a59a" name="company">企业单位</u-radio>
					<u-radio active-color="#14a59a" name="personage">个人/非企业单位</u-radio>
				</u-radio-group>
			</view>
		</view>
		<template v-if="type=='company'">
			<view class="main">
				<view class="form-item">
					<view class="item-left">
						<text>公司名称</text>
						<text class="required">*</text>
					</view>
					<view class="item-right">
						<u-input v-model="form.name" placeholder="请输入公司名称" height="80" />
					</view>
				</view>
				<view class="form-item">
					<view class="item-left">
						<text>公司税号</text>
						<text class="required">*</text>
					</view>
					<view class="item-right">
						<u-input v-model="form.tax" placeholder="填写纳税人识别号" height="80" />
					</view>
				</view>
				<view class="form-item">
					<view class="item-left">
						<text>注册地址</text>
					</view>
					<view class="item-right">
						<u-input v-model="form.address" placeholder="请填写公司注册地址" height="80" />
					</view>
				</view>
				<view class="form-item">
					<view class="item-left">
						<text>注册电话</text>
					</view>
					<view class="item-right">
						<u-input v-model="form.registerPhone" placeholder="请填写公司注册电话" height="80" />
					</view>
				</view>
				<view class="form-item">
					<view class="item-left">
						<text>开户银行</text>
					</view>
					<view class="item-right">
						<u-input v-model="form.depositBank" placeholder="请填写公司开户银行" height="80" />
					</view>
				</view>
				<view class="form-item">
					<view class="item-left">
						<text>银行账号</text>
					</view>
					<view class="item-right">
						<u-input v-model="form.bankAccount" placeholder="请填写银行账号" height="80" />
					</view>
				</view>
			</view>
			<view class="bottom-form">
				<view class="switch">
					<view class="switch-left">
						<view class="switch-title">设为默认抬头</view>
						<view class="switch-subtitle">每次开票会默认填写该抬头信息</view>
					</view>
					<view class="switch-right">
						<u-switch v-model="form.switchChecked" active-color="#0DA297"></u-switch>
					</view>
				</view>
				<view class="form-item">
					<view class="item-left">
						<text>联系电话</text>
						<text class="required">*</text>
					</view>
					<view class="item-right">
						<u-input v-model="form.phone" placeholder="请填写个人手机号" height="80" />
					</view>
				</view>
				<view class="form-item">
					<view class="item-left">
						<text>邮箱地址</text>
						<text class="required">*</text>
					</view>
					<view class="item-right">
						<u-input v-model="form.email" placeholder="填写个人邮箱接收发票" height="80" />
					</view>
				</view>
			</view>
		</template>
		<template v-else>
			<view class="main">
				<view class="form-item">
					<view class="item-left">
						<text>抬头名称</text>
						<text class="required">*</text>
					</view>
					<view class="item-right">
						<u-input v-model="personageForm.name" placeholder="请输入抬头名称" height="80" />
					</view>
				</view>
			</view>
			<view class="bottom-form">
				<view class="switch">
					<view class="switch-left">
						<view class="switch-title">设为默认抬头</view>
						<view class="switch-subtitle">每次开票会默认填写该抬头信息</view>
					</view>
					<view class="switch-right">
						<u-switch v-model="personageForm.switchChecked" active-color="#0DA297"></u-switch>
					</view>
				</view>
			</view>
		</template>
		<!--btn-->
		<view class="btn-wrap">
			<view class="btn">
				<uv-btn @click="handleSaveClick" background="#0DA297" text="保 存" :loading="loading"
					loading-text="保存中..."></uv-btn>
			</view>
			<view class="bottom-safe-area"></view>
		</view>
		<view class="bottom-safe-area"></view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				//上一页数据
				prevData:{
					ids:'',
					all:'',
					finalNum:'',
					finalPrice:'',
				},

				type: 'company',
				//公司
				form: {
					name: "",
					tax: '',
					address: '',
					registerPhone: '',
					depositBank: '',
					bankAccount: '',
					switchChecked: false,
					phone: '',
					email: '',
				},
				loading: false,
				//个人
				personageForm:{
					name:'',
					switchChecked: false,
				}
			}
		},
		onLoad(options) {
			this.prevData=uni.getStorageSync('invoicePage');//上一页数据
			this.getInvoiceRequest();//查询默认发票数据
			
		},
		onUnload() {
			if(uni.getStorageSync('invoicePage')){
				 uni.removeStorageSync('invoicePage');
			};
		},
		methods: {
			handleSaveClick() {
				let prevData={
					ids:this.prevData.ids,
					total_num:this.prevData.finalNum,
					total_money:this.prevData.finalPrice,
					select_all:this.prevData.all,
				};				
				if(this.type=='company'){
					// 处理switchChecked
					if(this.$utils.isAllEmpty(this.form.name)){
						this.$utils.toast('公司名称不能为空');
					}else if(this.$utils.isAllEmpty(this.form.tax)){
						this.$utils.toast('公司税号不能为空');
					}else if(this.$utils.isAllEmpty(this.form.phone)){
						this.$utils.toast('联系电话不能为空');
					}else if(this.$utils.isAllEmpty(this.form.email)){
						this.$utils.toast('邮箱地址不能为空');
					}else if(!this.$utils.isPhone(this.form.phone)){
						this.$utils.toast('联系电话格式不正确');
					}else{
						//request
						let data={
							type:'1',
							...prevData,
							company_name:this.form.name,
							company_tax:this.form.tax,
							company_address:this.form.address,
							company_phone:this.form.registerPhone,
							company_bank:this.form.depositBank,
							company_bank_num:this.form.bankAccount,
							default:this.form.switchChecked?"1":"0",
							phone:this.form.phone,
							email:this.form.email,
						};
						this.sureRequest(data);
					};
				}else if(this.type=='personage'){
					if(this.$utils.isAllEmpty(this.personageForm.name)){
						this.$utils.toast('抬头名称不能为空');
					}else{
						//request 
						let data={
							type:'2',
							...prevData,
							person_name:this.personageForm.name,
							default:this.personageForm.switchChecked?"1":"0",
						};
						this.sureRequest(data);
					};
				};
			},
			sureRequest(data){
				
				this.loading=true;
				this.$u.api.submitInvoice(data).then(res=>{
					this.loading=false;
					if(res.result_code==1000){
						this.$utils.modal({
							content:'保存成功！',
							showCancel:false,
							callback:(bool)=>{
								let pages = getCurrentPages(); // 获取页面栈
								let prevPage = pages[pages.length - 2]; // 上一个页面
								prevPage.$vm.allChecked = false;
								prevPage.$vm.allNum = 0;
								prevPage.$vm.allPrice = 0;
								prevPage.$vm.list = [];
								prevPage.$vm.pageChecked = false;
								prevPage.$vm.isNext = false;
								prevPage.$vm.getListRequest();
								this.$utils.back();
							}
						});
					}else{
						this.$utils.toast(res.msg);
					}
				}).catch(()=>{
					this.loading=false;
				});	
			},
			getInvoiceRequest(){
				this.$u.api.getInvoice().then(res=>{
				
					if(res.result_code==1000){
						if(res.data.type==1){
							//公司
							this.type='company';
							this.form={
								name:res.data.company_name,
								tax: res.data.company_tax,
								address:res.data.company_address,
								registerPhone:res.data.company_phone,
								depositBank:res.data.company_bank,
								bankAccount:res.data.company_bank_num,
								switchChecked: res.data.default==1,
								phone:res.data.phone,
								email:res.data.email,
							};
						}else if(res.data.type==2){
							//个人
							this.type='personage';
							this.personageForm={
								name:res.data.person_name,
								switchChecked: res.data.default==1,
							};
						};
					
					};
				}).catch();
			}
		}
	}
</script>
<style lang="scss" scoped>
	.header {
		padding: 20rpx 30rpx;
		border-bottom: 16rpx solid #F4F5F7;
	}

	.header-item {
		vertical-align: middle;
		display: inline-block;
	}

	.header-item:nth-child(1) {
		padding-right: 30rpx;
		font-size: 30rpx;
		color: #606266;

	}

	.form-item {
		padding-left: 240rpx;
		border-bottom: 2rpx solid #E5E5E5;
		position: relative;
	}

	.item-left {
		position: absolute;
		left: 30rpx;
		top: 50%;
		transform: translateY(-50%);
		font-size: 28rpx;
		color: #333;
	}

	.item-left .required {
		color: #0DA297;
		padding-left: 10rpx;
		font-size: 32rpx;
	}
	.bottom-form{
		border-top: 16rpx solid #F4F5F7;
	}
	.switch {
		position: relative;
	}

	.switch-right {
		position: absolute;
		right: 30rpx;
		top: 50%;
		transform: translateY(-50%);
	}

	.switch-title {
		font-size: 28rpx;
		color: #333333;
	}

	.switch-subtitle {
		font-size: 18rpx;
		color: #A6A6A6;
	}

	.switch {
		border-bottom: 2rpx solid #E5E5E5;
		padding: 20rpx 30rpx;
	}
	.btn-wrap{
		position:fixed;
		left:0;
		bottom:0;
		box-sizing: border-box;
		padding:20rpx 40rpx;
		width:100%;
		background-color:#fff;
	}
</style>
